<?php
include_once ("../Login/databaseconnection.php");

 $Training_Name=$_POST['Training_Name'];
	   	  $resultnet=mysql_query("SELECT * FROM training WHERE Training_Name='$Training_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Training_Id=$rownet['Training_Id'];
}
	  
	  $TeaName=$_POST['TeaName'];
$name=explode(" ",$TeaName);
$fn=$name[0];
$ln=$name[1];
$results= mysql_query("SELECT * FROM teacher Where First_Name='$fn' AND Last_Name='$ln'");
if($row = mysql_fetch_array($results))
{
 $Teacher_NIC=$row['T_NIC_Number'];
}
  if($Training_Id!="Select One"&&$Teacher_NIC!="Select One")
	   {
	   $result=mysql_query("SELECT * FROM training_details WHERE Training_Id='$Training_Id' AND Teacher_NIC='$Teacher_NIC'");
	   	 	  if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
$Training_Id=$row['Training_Id'];
$Teacher_NIC=$row['Teacher_NIC'];
$Duration=$row['Duration'];
  $res=mysql_query("SELECT * FROM training WHERE Training_Id='$Training_Id'");
  if($rowp=mysql_fetch_array($res))
  {
  $Training_Name=$rowp['Training_Name'];
  }
$res=mysql_query("SELECT * FROM  training WHERE Training_Id='$Training_Id'");
if($rowc=mysql_fetch_array($res))
{
$Training_Name=$rowc['Training_Name'];
}
		 }
	   if(mysql_num_rows($result)==0)
   {

	   $_SESSION['errornorow']="Very sorry there is no result,Please check your inputs and try again!";
   }
   else
   {
    if($_SESSION['Access_Type']=="Administrator")
   {
	 $del=mysql_query("DELETE FROM training_details WHERE Training_Id='$Training_Id' AND Teacher_NIC='$Teacher_NIC'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
  }
 if(mysql_affected_rows()==0)
  {
$Duration='';
$Training_Name='';
 $_SESSION['error']="Sorry unable to delete this record!";
  }
   }
    if($_SESSION['Access_Type']=="Teacher in Charge")
   {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
  if($rowsch=mysql_fetch_array($get_Sch))
{
  $Own_School_Id=$rowsch['School_Id'];
  $resultte=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$Teacher_NIC'");
   if($rowscht=mysql_fetch_array($resultte))
{
$School_Id=$rowscht['School_Id'];
	if($School_Id==$Own_School_Id)
	{
	date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
	$Accessed_By=$_SESSION['NIC_Number'];
	if($row=mysql_fetch_array($result))
   {
   }
  $sql=mysql_query("INSERT INTO deleted_training_details(Training_Id,Teacher_NIC,Duration,Deleted_date,Accessed_By,Last_Updated_Date) VALUES('$Training_Id','$Teacher_NIC','$Duration','$t_d','$Accessed_By','$Last_Updated_Date')");
if($sql)
{
$del=mysql_query("DELETE FROM training_details WHERE Training_Id='$Training_Id' AND Teacher_NIC='$Teacher_NIC'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
  }
 if(mysql_affected_rows()==0)
  {
$Duration='';
$Training_Name='';
 $_SESSION['error']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_training_details WHERE Teacher_NIC='$Teacher_NIC' AND Deleted_Date='$t_d' AND Training_Id='$Training_Id'");
  }
}
   if(!$sql)
{
$Duration='';
$Training_Name='';
 $_SESSION['error']="Sorry unable to delete this record!";
 }
	}
	else
  {
$Duration='';
$Training_Name='';
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
  }
  }
  else
  {
$Duration='';
$Training_Name='';
  $_SESSION['errornot']="This person's record is not registered into Teacher's record!";
  }
	}
		else
  {
$Duration='';
$Training_Name='';
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
  }
	}
    }
  }
  }
 if($Training_Id=="Select One"||$Teacher_NIC=="Select One" )
	   {
		   $_SESSION['errorin']="Please select the Training Id,Teacher NIC and Year!";
   	   }  
 ?>